#include "Cond.h"
#include "platform/Platform.h"
#include "pthread.h"

namespace easy2d {


	Cond::Cond()
	{
		pthread_cond_init(&mCond, NULL);
	}

	Cond::~Cond()
	{
		pthread_cond_destroy(&mCond);
	}

	void Cond::broadcast()
	{
		pthread_cond_broadcast(&mCond);
	}

	void Cond::signal()
	{
		pthread_cond_signal(&mCond);
	}

	void Cond::wait(Mutex& mutex)
	{
		pthread_cond_wait(&mCond, &mutex.mMutex);
	}

	void Cond::timedwait(Mutex& mutex, uint milliseconds)
	{
		uint sec = (int)((float)milliseconds / 1000.0f);
		uint mill = milliseconds - sec * 1000;

		timespec outtime;
		outtime.tv_sec = (long)Platform::now() + sec;
		outtime.tv_nsec = mill * 1000;
		pthread_cond_timedwait(&mCond, &mutex.mMutex, &outtime);
	}

}	// namespace


